﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class ttss : MonoBehaviour {
    private List<string> shuang= new List<string>();//ssq配置
    private List<string> daletou = new List<string>();//dlt配置

    private List<List<int>> ssqoldlist = new List<List<int>>();
    private List<List<int>> dltoldlist = new List<List<int>>();

    private int[] ssqnew = new int[7];//当前最新ssq号new int[7] { 10, 12, 14, 18, 23, 1, 4}
    private int[] dltnew = new int[7];//当前最新dlt号new int[7] { 10, 12, 14, 18, 23, 1, 4}

    [SerializeField]
    private Text ssqtf;
    [SerializeField]
    private Text dlttf;

    private List<int> ssqblue = new List<int>();
    private List<int> ssqred = new List<int>();
    private List<int> dltblue = new List<int>();
    private List<int> dltred = new List<int>();

    private List<int> datelist = new List<int>();
    // Use this for initialization
    void Start () {
        ////读取配置
        //StreamReader sr = new StreamReader(Application.streamingAssetsPath + "/shuang.txt", Encoding.UTF8);
        //string line;
        //int iddx = 0;
        //while ((line = sr.ReadLine()) != null)
        //{
        //    shuang.Add(line);
        //    iddx++;
        //}
        ////TextAsset ssqasset = Resources.Load("shuang", typeof(TextAsset)) as TextAsset;
        ////shuang = ssqasset.text.Trim().Split('\n');
        //for (int i=0;i< shuang.Count;i++)
        //{
        //    string[] ss = shuang[i].Split(':')[1].Split('-');
        //    List<int> ds = new List<int>();
        //    for(int j=0;j<ss.Length;j++)
        //    {
        //        ds.Add(int.Parse(ss[j]));
        //    }
        //    ssqoldlist.Add(ds);
        //}
        //for(int k=0;k< ssqoldlist[ssqoldlist.Count-1].Count;k++)
        //{
        //    ssqnew[k] = ssqoldlist[ssqoldlist.Count - 1][k];
        //}
        //for(int i=1;i<=33;i++)
        //{
        //    ssqblue.Add(i);
        //}
        //for (int j = 1; j <= 16; j++)
        //{
        //    ssqred.Add(j);
        //}
        ////屏蔽掉最新一期所有号
        //for (int i = 0; i < 6; i++)
        //{
        //    for (int j = 0; j < ssqblue.Count; j++)
        //    {
        //        if (ssqnew[i] == ssqblue[j])
        //        {
        //            ssqblue.RemoveAt(j);
        //            break;
        //        }
        //    }
        //}
        //ssqred.RemoveAt(ssqnew[6] - 1);

        //StreamReader sr2 = new StreamReader(Application.streamingAssetsPath + "/daletou.txt", Encoding.UTF8);
        //string line2;
        //int iddx2 = 0;
        //while ((line2 = sr2.ReadLine()) != null)
        //{
        //    daletou.Add(line2);
        //    iddx2++;
        //}
        ////TextAsset dltasset = Resources.Load("daletou", typeof(TextAsset)) as TextAsset;
        ////daletou = dltasset.text.Trim().Split('\n');
        //for (int i = 0; i < daletou.Count; i++)
        //{
        //    string[] ss = daletou[i].Split(':')[1].Split('-');
        //    List<int> ds = new List<int>();
        //    for (int j = 0; j < ss.Length; j++)
        //    {
        //        ds.Add(int.Parse(ss[j]));
        //    }
        //    dltoldlist.Add(ds);
        //}
        //for (int k = 0; k < dltoldlist[dltoldlist.Count - 1].Count; k++)
        //{
        //    dltnew[k] = dltoldlist[dltoldlist.Count - 1][k];
        //}
        //for (int i = 1; i <= 35; i++)
        //{
        //    dltblue.Add(i);
        //}
        //for (int j = 1; j <= 12; j++)
        //{
        //    dltred.Add(j);
        //}
        ////屏蔽掉最新一期所有号
        //for (int i = 0; i < 5; i++)
        //{
        //    for (int j = 0; j < dltblue.Count; j++)
        //    {
        //        if (dltnew[i] == dltblue[j])
        //        {
        //            dltblue.RemoveAt(j);
        //            break;
        //        }
        //    }
        //}
        //for (int i = 0; i < 2; i++)
        //{
        //    for (int j = 0; j < dltred.Count; j++)
        //    {
        //        if (dltnew[5 + i] == dltred[j])
        //        {
        //            dltred.RemoveAt(j);
        //            break;
        //        }
        //    }
        //}

        //ssqtf.text = "";
        //dlttf.text = "";

        for (int m=0;m<10;m++)
        {
            datelist.Add(m);
        }
        this.transform.Find("Scroll View").GetComponent<ListUI>().Data(datelist);

        Invoke("additem", 3);
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void ClickSSQ()
    {
        Debug.Log("随机出SSQ");
        //开始随机
        SSQRandom();
    }

    private void SSQRandom()
    {
        ssqtf.text = "";
        for (int i=0;i<5;i++)
        {
            //随机6个
            Dictionary<int, int> dic = new Dictionary<int, int>();
            int idx = 0;
            while (idx < 6)
            {
                int ran = UnityEngine.Random.Range(1, ssqblue.Count);
                if (!dic.ContainsKey(ssqblue[ran]))
                {
                    dic.Add(ssqblue[ran], ssqblue[ran]);
                    idx++;
                }
            }
            //随机1个
            int dom = UnityEngine.Random.Range(0, ssqred.Count);
            List<int> list = new List<int>();
            foreach(var s in dic)
            {
                list.Add(s.Key);
            }
            list.Sort();
            list.Add(ssqred[dom]);
            if(chackSsqHas(list))
            {
                i--;
                Debug.Log("ssq随机到了以前中过的号");
            }
            else
            {
                for(int k=0;k< list.Count;k++)
                {
                    ssqtf.text += list[k] + (k== list.Count-1?"":"-");
                }
                ssqtf.text += "\n";
            }
        }
    }

    private bool chackSsqHas(List<int> lis)
    {
        for(int i=0;i< ssqoldlist.Count;i++)
        {
            int con = 0;
            for(int j=0;j< lis.Count;j++)
            {
                if (ssqoldlist[i].Contains(lis[j]))
                {
                    con++;
                }
            }
            if(con == lis.Count)
            {
                return true;
            }
        }
        return false;
    }

    public void ClickDLT()
    {
        Debug.Log("随机出DLT");
        //开始随机
        DLTRandom();
    }

    private void DLTRandom()
    {
        dlttf.text = "";
        for (int i = 0; i < 5; i++)
        {
            //随机5个
            Dictionary<int, int> dic = new Dictionary<int, int>();
            int idx = 0;
            while (idx < 5)
            {
                int ran = UnityEngine.Random.Range(1, dltblue.Count);
                if (!dic.ContainsKey(dltblue[ran]))
                {
                    dic.Add(dltblue[ran], dltblue[ran]);
                    idx++;
                }
            }
            //随机2个
            int dom = UnityEngine.Random.Range(0, dltred.Count);
            int dom2 = UnityEngine.Random.Range(0, dltred.Count);
            while (dom == dom2)
            {
                dom2 = UnityEngine.Random.Range(0, dltred.Count);
            }
            List<int> list = new List<int>();
            foreach (var s in dic)
            {
                list.Add(s.Key);
            }
            list.Sort();
            if (dom < dom2)
            {
                list.Add(dltred[dom]);
                list.Add(dltred[dom2]);
            }
            else
            {
                list.Add(dltred[dom2]);
                list.Add(dltred[dom]);
            }
            if (chackDltHas(list))
            {
                i--;
                Debug.Log("dlt随机到了以前中过的号");
            }
            else
            {
                for (int k = 0; k < list.Count; k++)
                {
                    dlttf.text += list[k] + (k == list.Count - 1 ? "" : "-");
                }
                dlttf.text += "\n";
            }
        }
    }

    private bool chackDltHas(List<int> lis)
    {
        for (int i = 0; i < dltoldlist.Count; i++)
        {
            int con = 0;
            for (int j = 0; j < lis.Count; j++)
            {
                if (dltoldlist[i].Contains(lis[j]))
                {
                    con++;
                }
            }
            if (con == lis.Count)
            {
                return true;
            }
        }
        return false;
    }

    public void ClickAdd()
    {
        for (int m = 0; m < 10; m++)
        {
            datelist.Add(datelist.Count);
        }
        this.transform.Find("Scroll View").GetComponent<ListUI>().Data(datelist);
    }

    public void ClickCut()
    {
        for (int m = 0; m < 5; m++)
        {
            datelist.RemoveAt(datelist.Count - 1);
        }
        this.transform.Find("Scroll View").GetComponent<ListUI>().Data(datelist);
    }

    private void additem()
    {
        for (int m = 0; m < 2; m++)
        {
            datelist.Add(datelist.Count);
        }
        this.transform.Find("Scroll View").GetComponent<ListUI>().Data(datelist);
        Invoke("additem", 3);
    }
}
